/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.Businesses;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Lee
 */
public class BusinessDAO extends DAO {

    public BusinessDAO() {
        super();
    }

//BASIC DAO METHODS
    @SuppressWarnings("unchecked")
    public List<Businesses> getAll() throws Exception {
        List<Businesses> a = (List<Businesses>) getSession().createQuery("from Businesses a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Businesses get(int id) throws Exception {
        Businesses a = (Businesses) getSession().createQuery("from Businesses a where a.businessid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Businesses a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Businesses a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Businesses> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Businesses> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }
// USE get(int id) above...
//    @SuppressWarnings("unchecked")
//    public Businesses getById(int id) throws Exception {
//        return (Businesses) session.createQuery("from Businesses b where b.businessid=:id").setParameter("id", id).setMaxResults(1).uniqueResult();
//    }
// USE update(Businesses a) above...
//    @SuppressWarnings("unchecked")
//    public boolean updateBusiness(Businesses b, int busid) {
//        try {
//            session.createQuery("update Businesses b set b.businessname=:bn, b.businesstype=:bt, b.floorarea=:fa, "
//                    + "b.lotarea=:la, b.stability=:s, b.model=:mo, b.make=:ma, b.land=:l,"
//                    + "b.dailyaveragesales=:das, b.landlordaddress=:lla, b.landlordname=:ln, b.landlordphoneno=:lp,"
//                    + "b.monthlyrental=:mr where b.businessid=:bid").setParameter("bn", b.getBusinessname()).setParameter("bt", b.getBusinesstype()).setParameter("fa", b.getFloorarea()).setParameter("la", b.getLotarea()).setParameter("s", b.getStability()).setParameter("mo", b.getModel()).setParameter("ma", b.getMake()).setParameter("l", b.getLand()).setParameter("das", b.getDailyaveragesales()).setParameter("lla", b.getLandlordaddress()).setParameter("ln", b.getLandlordname()).setParameter("lp", b.getLandlordphoneno()).setParameter("mr", b.getMonthlyrental()).setParameter("bid", busid).executeUpdate();
//
//
////            bus.setMonthlyrental(Integer.parseInt(businessmonthlyrental[i]));
//            return true;
//        } catch (Exception ex) {
//            transaction.rollback();
//            ex.printStackTrace();
//            return false;
//        }
//
//    }
}
